<script setup>
import { RouterLink, RouterView, useRouter } from 'vue-router'
import { computed, onMounted } from 'vue'
import { useAuthStore } from './stores/auth'

const router = useRouter()
const authStore = useAuthStore()

onMounted(() => {
  authStore.hydrate()
})

const isAuthenticated = computed(() => authStore.isAuthenticated)
const displayName = computed(() => authStore.currentUser?.name ?? '')

function handleLogout() {
  authStore.logout()
  router.push('/')
}
</script>

<template>
  <div id="app">
    <header class="main-header">
      <div class="container">
        <div class="header-content">
          <div class="logo-section">
            <h1 class="brand-title">FitMatch Pro</h1>
            <p class="brand-subtitle">专业健身私教匹配平台</p>
          </div>

          <nav class="main-nav">
            <RouterLink to="/" class="nav-link">首页</RouterLink>
            <RouterLink to="/trainers" class="nav-link">私教团队</RouterLink>
            <RouterLink to="/matching" class="nav-link">智能匹配</RouterLink>
            <RouterLink to="/booking" class="nav-link">预约课程</RouterLink>
            <RouterLink to="/dashboard" class="nav-link">数据面板</RouterLink>
            <RouterLink to="/quality" class="nav-link">质量保障</RouterLink>
            <RouterLink to="/security" class="nav-link">安全中心</RouterLink>
            <RouterLink
              v-if="isAuthenticated"
              to="/profile"
              class="nav-link"
            >
              个人中心
            </RouterLink>
          </nav>

          <div class="auth-actions">
            <div v-if="isAuthenticated" class="user-chip">
              <span class="user-name">{{ displayName }}</span>
              <button class="logout-btn" @click="handleLogout">
                退出
              </button>
            </div>
            <div v-else class="guest-links">
              <RouterLink to="/login" class="auth-link">登录</RouterLink>
              <RouterLink to="/register" class="auth-link primary">注册</RouterLink>
            </div>
          </div>
        </div>
      </div>
    </header>

    <main class="main-content">
      <RouterView />
    </main>

    <footer class="main-footer">
      <div class="container">
        <p>&copy; 2024 FitMatch Pro. 专业健身私教匹配平台</p>
      </div>
    </footer>
  </div>
</template>

<style scoped>
#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 30px;
  width: 100%;
}

.main-header {
  background: linear-gradient(135deg, #0f0f23 0%, #1a1a2e 50%, #16213e 100%);
  color: white;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  position: relative;
  overflow: hidden;
}

.main-header::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(0, 255, 255, 0.1), transparent);
  animation: shimmer 3s infinite;
}

@keyframes shimmer {
  0% {
    left: -100%;
  }
  100% {
    left: 100%;
  }
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.2rem 0;
  position: relative;
  z-index: 2;
  gap: 1.5rem;
}

.logo-section {
  flex: 1;
}

.brand-title {
  font-size: 2.2rem;
  font-weight: 800;
  margin: 0;
  background: linear-gradient(45deg, #00f5ff, #ff00ff, #00ff00);
  background-size: 200% 200%;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  animation: gradientShift 4s ease-in-out infinite;
  text-shadow: 0 0 30px rgba(0, 245, 255, 0.5);
}

@keyframes gradientShift {
  0%,
  100% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
}

.brand-subtitle {
  font-size: 0.95rem;
  margin: 0;
  color: #00f5ff;
  opacity: 0.8;
  font-weight: 500;
  text-shadow: 0 0 10px rgba(0, 245, 255, 0.3);
}

.main-nav {
  display: flex;
  gap: 1.2rem;
  align-items: center;
}

.nav-link {
  color: #ffffff;
  text-decoration: none;
  padding: 0.8rem 1.4rem;
  border-radius: 30px;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  font-weight: 600;
  font-size: 0.95rem;
  position: relative;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  overflow: hidden;
}

.nav-link::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(0, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.nav-link:hover {
  background: rgba(0, 255, 255, 0.1);
  border-color: #00f5ff;
  transform: translateY(-3px) scale(1.05);
  box-shadow: 0 10px 25px rgba(0, 245, 255, 0.3);
  text-shadow: 0 0 15px rgba(0, 245, 255, 0.8);
}

.nav-link:hover::before {
  left: 100%;
}

.nav-link.router-link-active {
  background: linear-gradient(135deg, rgba(0, 255, 255, 0.2), rgba(255, 0, 255, 0.2));
  border-color: #00f5ff;
  box-shadow: 0 0 20px rgba(0, 245, 255, 0.4), inset 0 0 20px rgba(255, 0, 255, 0.1);
  text-shadow: 0 0 10px rgba(0, 245, 255, 0.8);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%,
  100% {
    box-shadow: 0 0 20px rgba(0, 245, 255, 0.4), inset 0 0 20px rgba(255, 0, 255, 0.1);
  }
  50% {
    box-shadow: 0 0 30px rgba(0, 245, 255, 0.6), inset 0 0 30px rgba(255, 0, 255, 0.2);
  }
}

.auth-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.auth-link {
  color: #ffffff;
  text-decoration: none;
  padding: 0.6rem 1.2rem;
  border-radius: 24px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: rgba(255, 255, 255, 0.08);
  transition: all 0.3s ease;
  font-weight: 600;
  font-size: 0.9rem;
}

.auth-link:hover {
  background: rgba(0, 245, 255, 0.12);
  border-color: #00f5ff;
  box-shadow: 0 8px 20px rgba(0, 245, 255, 0.25);
}

.auth-link.primary {
  background: linear-gradient(135deg, rgba(0, 245, 255, 0.2), rgba(255, 0, 255, 0.2));
  border-color: rgba(0, 245, 255, 0.4);
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  padding: 0.6rem 1.2rem;
  border-radius: 24px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: rgba(255, 255, 255, 0.08);
}

.user-name {
  font-weight: 600;
  color: #00f5ff;
}

.logout-btn {
  background: transparent;
  border: none;
  color: rgba(255, 255, 255, 0.8);
  cursor: pointer;
  font-weight: 600;
  transition: color 0.2s ease;
}

.logout-btn:hover {
  color: #ff6b6b;
}

.main-content {
  flex: 1;
  padding: 3rem 0;
  background: #f8fafc;
  min-height: calc(100vh - 200px);
}

.main-footer {
  background: #2d3748;
  color: white;
  text-align: center;
  padding: 1rem 0;
  margin-top: auto;
}

@media (max-width: 1024px) {
  .header-content {
    flex-direction: column;
    align-items: flex-start;
  }

  .main-nav {
    flex-wrap: wrap;
    justify-content: center;
  }

  .auth-actions {
    align-self: center;
  }
}

@media (max-width: 768px) {
  .header-content {
    align-items: center;
    text-align: center;
  }

  .logo-section {
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .main-nav {
    flex-direction: column;
    gap: 0.8rem;
  }

  .auth-actions {
    flex-direction: column;
    gap: 0.6rem;
  }
}
</style>
